package com.alipay.freeblog.dal.ibatis;

import com.alipay.freeblog.dal.daoobject.TemplateDO;
import com.alipay.freeblog.dal.interfaces.TemplateDao;
import com.ibatis.sqlmap.client.SqlMapClient;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * 模版的ibatis dao 实现类
 * User: ning.ln
 * Date: 11-9-24
 * Time: 下午7:16
 */
@Repository("templateDao")
public class IbatisTemplateDaoImpl implements TemplateDao {


	private SqlMapClient sqlMapClient;

	@Override
	public boolean updateTemplate(TemplateDO templateDO) throws SQLException {
		return sqlMapClient.update("updateTemplate", templateDO) > 0;
	}

	@Override
	public List<TemplateDO> getTemplateListByName(String name) throws SQLException {
		if (name == null) {
			throw new SQLException("name can't be null");
		}
		List<?> list = sqlMapClient.queryForList("queryTemplateListByName", name);
		return (List<TemplateDO>) list;
	}

	/**
	 * 根据 name 得到template
	 *
	 * @param name 模版名称  xxx.vm
	 * @return 模版DO对象
	 */
	@Override
	public TemplateDO getTemplateByName(String name) throws SQLException {
		if (name == null) {
			throw new SQLException("name can't be null");
		}
		return (TemplateDO) sqlMapClient.queryForObject("queryTemplateByName", name);
	}

	public SqlMapClient getSqlMapClient() {
		return sqlMapClient;
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}

}
